package com.pshcp.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.util.StringUtils;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorDiagnosisEvaluationDTO;
import com.pshcp.domain.model.DoctorDiagnosisEvaluation;
import com.pshcp.domain.model.PatientFavor;
import com.pshcp.mapper.DoctorDiagnosisEvaluationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生诊断评价表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorDiagnosisEvaluationService {

    @Autowired
    private DoctorDiagnosisEvaluationMapper doctorDiagnosisEvaluationMapper;

    public int doctorDiagnosisEvaluationNum(Long doctorId) {
        Example example = new Example(PatientFavor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("doctorId", doctorId);
        return doctorDiagnosisEvaluationMapper.selectCountByExample(example);
    }

    public PageList<DoctorDiagnosisEvaluation> list(DoctorDiagnosisEvaluationDTO doctorDiagnosisEvaluationDTO) {
        if (doctorDiagnosisEvaluationDTO.getPageNum() != null && doctorDiagnosisEvaluationDTO.getPageSize() != null) {
            PageHelper.startPage(doctorDiagnosisEvaluationDTO.getPageNum(), doctorDiagnosisEvaluationDTO.getPageSize());
        }
        Example example = new Example(DoctorDiagnosisEvaluation.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorDiagnosisEvaluationDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorDiagnosisEvaluationDTO.getStatus());
        }
        if(doctorDiagnosisEvaluationDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", doctorDiagnosisEvaluationDTO.getDoctorId());
        }
        if(doctorDiagnosisEvaluationDTO.getAfterTime() != null){
            criteria.andGreaterThanOrEqualTo("createdTime", doctorDiagnosisEvaluationDTO.getAfterTime());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorDiagnosisEvaluation> list = doctorDiagnosisEvaluationMapper.selectByExample(example);
        if(!CollectionUtil.isEmpty(list)){
            for(DoctorDiagnosisEvaluation doctorDiagnosisEvaluation : list){
                doctorDiagnosisEvaluation.setPatientName(CommonTool.getNameResult(doctorDiagnosisEvaluation.getPatientName()));
            }
        }
        return CommonTool.buildPageList(list);
    }

    public List<DoctorDiagnosisEvaluation> select(DoctorDiagnosisEvaluation doctorDiagnosisEvaluation) {
        return doctorDiagnosisEvaluationMapper.select(doctorDiagnosisEvaluation);
    }

    public int insert(DoctorDiagnosisEvaluation doctorDiagnosisEvaluation) {
        doctorDiagnosisEvaluation.setId(SystemUtil.getId());
        doctorDiagnosisEvaluation.setStatus(CommonStatusEnum.INIT.getCode());
        doctorDiagnosisEvaluation.setCreatedTime(new Date());
        return doctorDiagnosisEvaluationMapper.insert(doctorDiagnosisEvaluation);
    }

    public int update(DoctorDiagnosisEvaluation doctorDiagnosisEvaluation) {
        doctorDiagnosisEvaluation.setUpdateTime(new Date());
        return doctorDiagnosisEvaluationMapper.updateByPrimaryKeySelective(doctorDiagnosisEvaluation);
    }

    public DoctorDiagnosisEvaluation findById(Long id) {
        return doctorDiagnosisEvaluationMapper.selectByPrimaryKey(id);
    }

    public List<DoctorDiagnosisEvaluation> findByIds(List<Long> ids) {
        Example example = new Example(DoctorDiagnosisEvaluation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorDiagnosisEvaluationMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorDiagnosisEvaluation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorDiagnosisEvaluation doctorDiagnosisEvaluation = new DoctorDiagnosisEvaluation();
        doctorDiagnosisEvaluation.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorDiagnosisEvaluationMapper.updateByExampleSelective(doctorDiagnosisEvaluation, example);
    }

    public int listCount(DoctorDiagnosisEvaluationDTO doctorDiagnosisEvaluationDTO){
        Example example = new Example(DoctorDiagnosisEvaluation.class);
        Example.Criteria criteria = example.createCriteria();
        if(doctorDiagnosisEvaluationDTO.getStatus() != null){
            criteria.andEqualTo("status", doctorDiagnosisEvaluationDTO.getStatus());
        }else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(doctorDiagnosisEvaluationDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", doctorDiagnosisEvaluationDTO.getDoctorId());
        }
        return doctorDiagnosisEvaluationMapper.selectCountByExample(example);
    }

    // 根据业务id查询数据
    public DoctorDiagnosisEvaluation queryByDataId(Long dataId) {
        Example example = new Example(DoctorDiagnosisEvaluation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("dataId", dataId);
        List<DoctorDiagnosisEvaluation> lstData = doctorDiagnosisEvaluationMapper.selectByExample(example);
        if(CollectionUtil.isEmpty(lstData)){
            return null;
        }
        return lstData.get(0);
    }
}
